<?php

// Include config file
include('./common.php');

// Connect to database
$link = dbConnect();

//see if the page and forumID are set
if (!isset($_POST['page']) || empty($_POST['page'])) {
    $page = 1;
}else{
	$page = mysql_real_escape_string($_POST['page']);
}

//if the forum ID isn't set, throw them back home
if (!isset($_POST['forumID'])) {
	echo "output=forumNotSet";
	return;
}else{
	$forumID = mysql_real_escape_string($_POST['forumID']);
}

//clean up the stuff coming from flash
$maxDaysNewThread = time() - 60*60*24*$_POST['maxDaysNewThread'];
$threadsPerPage = mysql_real_escape_string($_POST['threadsPerPage']);
$guestGroupID = mysql_real_escape_string($_POST['guestGroupID']);

//make sure that we have a forum!
$result = mysql_query("SELECT f.title FROM ".TABLE_PREFIX."_forums f WHERE f.forumID = ".$forumID);
if(mysql_num_rows($result) == 0){
	echo "output=forumNotFound";
	mysql_close($link);
	return;
}

$data = mysql_fetch_object($result);
$forumTitle = urlencode(stripslashes($data->title));
$output = "forumTitle=".$forumTitle;

//if they can't view this forum then don't bother going on
if(isset($_POST['username'])){
	//get the userID from the login data
	$user = auth($_POST['username'], $_POST['password']);
	if($user == ""){
		echo "output=badLogin";
		return;
	}
	
	//setup a userID for the rest of the script
	$flashUserID = $user['id'];
}else{
	$flashUserID = -1;
}

//get the user's permissions for this forum
$perms = checkForumPermissions($forumID, $flashUserID, $guestGroupID);

if(!$perms){
	echo "output=permissionError";
	mysql_close($link);
	return;
}

//let flash know about the user's perms
$output .= $perms;

//get the amount of threds per page
$offset = ($page - 1) * $threadsPerPage;

//set up the amount of threads to fetch and get the page number from flash
$query = "SELECT SQL_CALC_FOUND_ROWS t.*
		  FROM ".TABLE_PREFIX."_threads t
		  WHERE t.forumID = ".$forumID."
		  OR t.displayOrder = ".ANNOUCEMENT_DISPLAY_ORDER."
		  ORDER BY t.displayOrder DESC, t.lastPost DESC 
		  LIMIT $offset,$threadsPerPage";
$result = mysql_query($query);

if(!$result) {
    fail("mySqlError");
}

//set up a quick query to get the total number of threads to send to flash
$totalResult = mysql_query("SELECT FOUND_ROWS() AS threadTotal");
$dataTotal = mysql_fetch_object($totalResult);
$pages = ceil($dataTotal->threadTotal / $threadsPerPage);
$output .= "&currentPage=$page&totalPages=$pages";

// Find out how many threads in this forum
$threadCount = mysql_num_rows($result);

// send to flash
$output .= "&threadCount=$threadCount&forumID=$forumID";

//let flash know of the announcment number
$output .= "&announcementDisplayNumber=".ANNOUCEMENT_DISPLAY_ORDER;

// For each thread returned...
for ($i = 0; $i < $threadCount; $i++)
{
    // Extract details from database
    $thread = mysql_fetch_object($result);
    $userID = $thread->userID;
	$lastUserID = $thread->lastUserID;
	
	//get the string of the usernames
	$userLook = mysql_query("SELECT u.username AS username, u2.username AS lastUsername 
							FROM ".TABLE_PREFIX."_users u, ".TABLE_PREFIX."_users u2 
							WHERE u.userID = $userID 
							AND u2.userID = $lastUserID");
	
	$name = mysql_fetch_object($userLook);

    // Add thread details to output
    $output .= "&thread" . $i . "threadID=" . $thread->threadID;
    $output .= "&thread" . $i . "topic=" . urlencode(stripslashes($thread->topic));
    $output .= "&thread" . $i . "topicStarter=" . urlencode(stripslashes($name->username));
	$output .= "&thread" . $i . "topicStarterID=" . $userID;
    $output .= "&thread" . $i . "replies=" . $thread->replies;
    $output .= "&thread" . $i . "views=" . $thread->views;
    $output .= "&thread" . $i . "lastPost=" . strftime("%m/%d/%y %I:%M %p", $thread->lastPost);
	$output .= "&thread" . $i . "lastUsername=" . stripslashes($name->lastUsername);
	$output .= "&thread" . $i . "sticky=" . $thread->displayOrder;
	$output .= "&thread" . $i . "locked=" . $thread->readMode;
	
	//set the default to no new posts
	$newPosts = "&thread" . $i . "newPosts=-1";
	
	//new posts?
	if(isset($flashUserID) && $thread->lastPost > $maxDaysNewThread){
		$lastViewedPost = newPostsInThread($flashUserID, $thread->threadID);
		if($lastViewedPost < $thread->lastPost){
			$newPosts = "&thread" . $i . "newPosts=".$lastViewedPost;	
		}
	}
	
	$output .= $newPosts;
}

echo $output;

// Close link to database server
mysql_close($link);

?>